﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow( QWidget* parent ) : QMainWindow( parent ), ui( new Ui::MainWindow ) {
    ui->setupUi( this );
}

MainWindow::~MainWindow() {
    delete ui;
}

void MainWindow::on_pushButton_clicked() {
    const char* hostname = "172.100.6.137";
    const char* username = "work";
    const char* password = ";;;";
    // 创建SSH会话
    ssh_session sshSession;
    sshSession = ssh_new();
    if ( !sshSession ) { qDebug() << "Error creating SSH session"; }
    // 设置SSH连接选项
    ssh_options_set( sshSession, SSH_OPTIONS_HOST, hostname );
    ssh_options_set( sshSession, SSH_OPTIONS_USER, username );
    // 连接SSH服务器
    int rc = ssh_connect( sshSession );
    if ( rc != SSH_OK ) {
        qDebug() << "Error connecting to SSH server: " << ssh_get_error( sshSession );
        ssh_free( sshSession );
    }
    // 验证SSH服务器主机密钥
    int state = ssh_is_server_known( sshSession );
    if ( state != SSH_SERVER_KNOWN_OK ) {
        qDebug() << "Error verifying SSH server host key: " << ssh_get_error( sshSession );
        ssh_disconnect( sshSession );
        ssh_free( sshSession );
    }
    // 验证SSH用户名和密码
    rc = ssh_userauth_password( sshSession, NULL, password );
    if ( rc != SSH_AUTH_SUCCESS ) {
        qDebug() << "Error authenticating with SSH server: " << ssh_get_error( sshSession );
        ssh_disconnect( sshSession );
        ssh_free( sshSession );
    }
    // 执行PBS命令获取作业调度系统状态信息
    ssh_channel channel = ssh_channel_new( sshSession );
    if ( !channel ) {
        qDebug() << "Error creating SSH channel";
        ssh_disconnect( sshSession );
        ssh_free( sshSession );
    }
    rc = ssh_channel_open_session( channel );
    if ( rc != SSH_OK ) {
        qDebug() << "Error opening SSH channel: " << ssh_get_error( sshSession );
        ssh_channel_free( channel );
        ssh_disconnect( sshSession );
        ssh_free( sshSession );
    }
    rc = ssh_channel_request_exec( channel, "ls" );
    if ( rc != SSH_OK ) {
        qDebug() << "Error executing PBS command: " << ssh_get_error( sshSession );
        ssh_channel_close( channel );
        ssh_channel_free( channel );
        ssh_disconnect( sshSession );
        ssh_free( sshSession );
    }
    // 读取PBS命令输出
    char    buffer[ 1024 ];
    int     nbytes;
    QString output;
    while ( ( nbytes = ssh_channel_read( channel, buffer, sizeof( buffer ), 0 ) ) > 0 ) {
        output += QString::fromUtf8( buffer, nbytes );
    }
    qDebug() << output;
    ui->textBrowser->append( output );
    if ( nbytes < 0 ) {
        qDebug() << "Error reading PBS command output: " << ssh_get_error( sshSession );
        ssh_channel_close( channel );
        ssh_channel_free( channel );
        ssh_disconnect( sshSession );
        ssh_free( sshSession );
    }
    // 关闭SSH会话和通道
    ssh_channel_send_eof( channel );
    ssh_channel_close( channel );
    ssh_channel_free( channel );
    ssh_disconnect( sshSession );
    ssh_free( sshSession );
    // 将PBS状态信息显示在textBrower控件中
}
